package com.example.workmode;

import com.example.workmode.UserPreferences.BreakDurationSetting;
import com.example.workmode.UserPreferences.RefreshTimeSetting;
import com.example.workmode.UserPreferences.WorkDurationSetting;

import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.R.menu;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

	ActionBar actionBar;
	ViewPager viewPager;
	FragmentPageAdapter fragmentPageAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager) findViewById(R.id.pager);
		fragmentPageAdapter = new FragmentPageAdapter(getSupportFragmentManager());
		
		// Set Action Bar
		actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.addTab(actionBar.newTab().setText(R.string.list).setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.app_name).setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.app_manager).setTabListener(this));
		actionBar.setSelectedNavigationItem(1);
		
		// Set ViewPager
		viewPager.setAdapter(fragmentPageAdapter);
		viewPager.setCurrentItem(1);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				actionBar.setSelectedNavigationItem(arg0);
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		viewPager.setCurrentItem(tab.getPosition());
		
	}
	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		
		switch(id) {
		case R.id.work_duration_settings:
			Intent wintent = new Intent(this, WorkDurationSetting.class);
			startActivity(wintent);
			return true;
		case R.id.break_duration_settings:
			Intent bintent = new Intent(this, BreakDurationSetting.class);
			startActivity(bintent);
			return true;
		case R.id.refresh_time_settings:
			Intent rintent = new Intent(this, RefreshTimeSetting.class);
			startActivity(rintent);
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	
	@Override
	public void onBackPressed() {
	    moveTaskToBack(true);
	}

	// Before 2.0
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        moveTaskToBack(true);
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}
